package Terminal.UI;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JPanel;



public class OptionPanelHorizzontal extends PanelComponent{
	private GridLayout gridlay; 
	
	public OptionPanelHorizzontal(PanelComponent parent){
		super(parent);
		pannellocomponente= new JPanel(/*new GridLayout()*/null);
		}
	
	public JPanel createComponents(){
		pannellocomponente.setBackground(Color.white);
		return pannellocomponente ;
	}

	public void add(PanelComponent figlio) {
		super.add(figlio);
		pannellocomponente.add(figlio.createComponents());	
		updateSize();
	}
	
	public void addPosition(PanelComponent figlio, int x){
		super.add(figlio);
		JPanel paneltemp= figlio.createComponents();
		//Dimension size = figlio.getSize();
		float ytempfloat = getSize().height/2-figlio.getSize().height/2;
		int ytemp = (int) ytempfloat;
		System.out.print("\n panelbutton pos"+ytemp);
		paneltemp.setBounds(x,0,figlio.getSize().width,figlio.getSize().height);
	
		pannellocomponente.add(paneltemp);
		
		updateSize();
		//pannellocomponente.validate();
	//	System.out.print("\n option"+pannellocomponente.getPreferredSize()+" "+pannellocomponente.getSize());
		
	}
	
	public void remove(int index ){
		super.remove(index);
		pannellocomponente.remove(index);
	}
	

	
	public void updateSize(){
		size_width=0;
		size_height=0;
		int i=0;
		Dimension d= new Dimension(size_width,size_height);
		for (PanelComponent pcelement: children) {
			d=pcelement.getSize();
			/*size_width=size_width+d.width+pannellocomponente.getComponent(i).getLocation().x;
			if(size_height<d.height){size_height=d.height;}//*/
			if(d.width+pannellocomponente.getComponent(i).getLocation().x>size_width){size_width=d.width+pannellocomponente.getComponent(i).getLocation().x;}
			if(d.height+pannellocomponente.getComponent(i).getLocation().y>size_height){size_height=d.height+pannellocomponente.getComponent(i).getLocation().y;}
		
			i++;
		}
		setSize(size_width, size_height);
		
		if(parent!=null)parent.updateSize();
	}

	public void setSize(int width,int height)
	{
	super.setSize(width, height);
	int i=0;
	Dimension d= new Dimension(size_width,size_height);
	for (PanelComponent pcelement: children) {
		d=pcelement.getSize();
		float ytempfloat = height/2-d.height/2;
		int ytemp = (int) ytempfloat;
		pannellocomponente.getComponent(i).setLocation(pannellocomponente.getComponent(i).getLocation().x,ytemp);
		System.out.print("\nsetoption"+height+" "+d.height+" "+ytemp+" "+pannellocomponente.getComponent(i).getLocation().y);
		
		//System.out.print("\n panelbutton pos"+ytemp);
		
	
		i++;
	}
	}
}
